﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Test
{
    public partial class AgentTree : System.Web.UI.Page
    {
        protected int uid;
        protected int month;
        protected string nickname;
        private string sqltpl = "select u.user_id, u.nickname, u.realname, a.is_agent_time from t_user u left join t_user_agent a on u.user_id=a.user_id";
        protected void Page_Load(object sender, EventArgs e)
        {
            uid = RequestUtil.GetInteger("uid");
            month = RequestUtil.GetInteger("month");
            if (uid > 0)
            {
                nickname = DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue("select nickname from t_user where user_id=" + uid, "").ToString();
            }
        }
        public void WriteTable()
        {
            if (uid < 1)
            {
                return;
            }
            Response.Write("<div style='max-height:620px; width:600px; overflow:auto'>");
            Response.Write("<table style=\"width:100 %\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
            Response.Write("<tr><th>代理ID</th><th>昵称</th><th>姓名</th><th>成为代理时间</th><th>层级</th></tr>");
            int count = WriteChildTable(uid);
            Response.Write("</table>");
            Response.Write("</div>");
            Response.Write("<div>共" + count + "人</div>");
        }
        public int WriteChildTable(int uid, int level = 0)
        {
            bool firstcell;
            object[] itemarray;
            string sqldate = "";
            if (month > 0)
            {
                int year = DateTime.Today.Year;
                string date1 = string.Format("{0}-{1}-1 0:0:0", year, month);
                string date2 = string.Format("{0}-{1}-{2} 23:59:59", year, month, DateTime.DaysInMonth(year, month));
                sqldate = string.Format(" and a.is_agent_time >='{0}' and a.is_agent_time <='{1}'", date1, date2);
            }
            string sqlstmt = string.Format("{0} where u.parent1_uid={1} and u.is_agent=1{2}", sqltpl, uid, sqldate);
            DataTable dtList = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
            //Response.Write(sqlstmt+"::"+dtList.Rows.Count + "<br />");
            int count = dtList.Rows.Count;
            foreach (DataRow r in dtList.Rows)
            {
                firstcell = true;
                Response.Write("<tr>");
                itemarray = r.ItemArray;
                foreach (object value in itemarray)
                {
                    if (firstcell)
                    {
                        Response.Write(string.Format("<td>{0}{1}</td>", string.Empty.PadLeft(level * 4, '.'), value));
                    }
                    else
                    {
                        Response.Write(string.Format("<td>{0}</td>", value));
                    }
                    firstcell = false;
                }
                Response.Write("<td>"+(level+1)+"级</td>");
                Response.Write("</tr>");
                count += WriteChildTable((int)r["user_id"], level + 1);
            }
            return count;
        }
    }
}